package com.ruoyi.sumfun.blog.controller;

import com.ruoyi.common.core.domain.R;


import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.file.utils.FileUploadUtils;
import com.ruoyi.sumfun.blog.tool.file.config.MinioConfig;
import com.ruoyi.sumfun.blog.tool.file.service.IMinioSysFileService;
import io.swagger.annotations.Api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.*;

/**
 * 文件请求处理
 * 
 * @author ruoyi
 */
@RestController
@Api(value = "主数据-文件上传下载" , tags = {"主数据-文件上传下载" })
@RequestMapping("/fileUpDown")
public class SysFileController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private IMinioSysFileService iMinioSysFileService;

    @Autowired
    private MinioConfig minioConfig;


    /**
     * 文件上传请求
     *(value = "file",required = false) MultipartFile[] file,
     */
    @RequestMapping("/fileUpdate")
    public AjaxResult upload(@RequestParam(value = "file", required = false) MultipartFile[] files) {
        try {
            List<String> uploadedFileNames = new ArrayList<>();
            for (MultipartFile file : files) {
                if (file.isEmpty()) {
                    continue; // 跳过空文件
                }
                String fileName = FileUploadUtils.extractFilename(file);
                try (InputStream inputStream = file.getInputStream()) {
                    fileUpload(fileName, inputStream, file); // 假设这个方法处理了文件上传并关闭流
                    uploadedFileNames.add(fileName); // 只记录上传的文件名
                }
            }
            return success(uploadedFileNames); // 假设success方法接受一个List作为参数
        } catch (Exception e) {
            // 记录异常并返回失败响应
            logger.error("文件上传失败", e);
            return error("文件上传失败"); // 假设error方法返回一个带有错误信息的AjaxResult
        }
    }
    /*
    * 上传方法
    * */
    public R fileUpload(String fileName,InputStream inputStream,MultipartFile file){
        try{
                String url= iMinioSysFileService.uploadFile(fileName,inputStream,file.getSize(),file.getContentType());
                System.out.println("源文件上传" + url);
        }catch (Exception e){
            System.out.println(e);
            return R.fail();
        }
        return R.ok();
    }
    public static void deleteFileByIO(String filePath) {
        File file = new File(filePath);
        File[] list = file.listFiles();
        if (list != null) {
            for (File temp : list) {
                deleteFileByIO(temp.getAbsolutePath());
            }
        }
        file.delete();
    }



}